﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="poll.ascx.cs" Inherits="Administrator_poll" %>

  <asp:SqlDataSource ID="PollDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:up1vnConnectionString %>"
        OnSelecting="PollDataSource_Selecting" SelectCommand="SELECT * FROM [Polls] WHERE ([PollID] = @PollID)">
        <SelectParameters>
            <asp:Parameter Name="PollID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    
    <asp:FormView ID="PollFormView" runat="server" DataKeyNames="PollID" DataSourceID="PollDataSource" OnDataBound="PollFormView_DataBound">
        <ItemTemplate>
            <asp:Label CssClass="Poll_DisplayText" ID="DisplayTextLabel" runat="server" Text='<%# Bind("DisplayText") %>'></asp:Label>
            
            <asp:Panel ID="pnlTakePoll" runat="server" CssClass="Poll_TakePoll">
                <asp:RadioButtonList ID="rblPollAnswer" runat="server" DataSourceID="PollAnswersDataSource"
                    DataTextField="DisplayText" DataValueField="PollAnswerID">
                </asp:RadioButtonList>
                
                <asp:SqlDataSource ID="PollAnswersDataSource" runat="server"
                    ConnectionString="<%$ ConnectionStrings:up1vnConnectionString %>" OnSelecting="PollAnswersDataSource_Selecting"
                    SelectCommand="SELECT * FROM [PollAnswers] WHERE ([PollID] = @PollID) ORDER BY [SortOrder]" 
                    InsertCommand="INSERT INTO UserResponses (Users_ID, PollAnswerID) VALUES (@Users_ID, @PollAnswerID)" OnInserting="PollAnswersDataSource_Inserting">
                    <SelectParameters>
                        <asp:Parameter Name="PollID" Type="Int32" />
                    </SelectParameters>
                    <InsertParameters>
                        <asp:Parameter Name="UserID" />
                        <asp:ControlParameter ControlID="rblPollAnswer" Name="PollAnswerID" PropertyName="SelectedValue" />
                    </InsertParameters>
                </asp:SqlDataSource>

                <asp:Button ID="btnSubmitVote" runat="server" OnClick="btnSubmitVote_Click" Text="Vote" />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="rblPollAnswer"
                    Display="Dynamic" ErrorMessage="You must first select an option."></asp:RequiredFieldValidator>
            </asp:Panel>
            
            <asp:Panel ID="pnlPollResults" runat="server" CssClass="Poll_PollResults">
                <asp:SqlDataSource ID="PollResultsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:up1vnConnectionString %>"
                    OnSelecting="PollResultsDataSource_Selecting" SelectCommand="SELECT a.PollAnswerID, a.PollID, a.DisplayText, a.SortOrder, COUNT(r.Users_ID) as Votes&#13;&#10;FROM PollAnswers a&#13;&#10;LEFT JOIN UserResponses r ON a.PollAnswerID = r.PollAnswerID&#13;&#10;WHERE a.PollID = @PollID&#13;&#10;GROUP BY a.PollAnswerID, a.PollID, a.DisplayText, a.SortOrder&#13;&#10;ORDER BY a.SortOrder">
                    <SelectParameters>
                        <asp:Parameter Name="PollID" />
                    </SelectParameters>
                </asp:SqlDataSource>
                
                <asp:DataList ID="resultsDataList" runat="server" DataKeyField="PollAnswerID" DataSourceID="PollResultsDataSource" OnDataBinding="resultsDataList_DataBinding" OnItemDataBound="resultsDataList_ItemDataBound">
                    <ItemTemplate>
                        <asp:Label ID="DisplayTextLabel" runat="server" Text='<%# Eval("DisplayText") %>'></asp:Label>
                        (<asp:Label ID="VotesLabel" runat="server" Text='<%# Eval("Votes") %>'></asp:Label>
                        votes)<br />
                        <asp:Image ID="PercentageImage" runat="server" Height="7px" ImageUrl="~/images/bar.JPG" />
                        <asp:Label ID="PercentageLabel" runat="server"></asp:Label><br />
                        <br />
                    </ItemTemplate>
                </asp:DataList>
                
                <br /><br />
                <asp:Label runat="server" ID="TotalVotesLabel" CssClass="Poll_TotalVotes"></asp:Label>
            </asp:Panel>
        </ItemTemplate>
    </asp:FormView>